<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Flow;
use App\Models\FlowHistory;
use App\Models\Reason;
use App\Models\Resume;
use App\Models\Step;
use App\Models\User;
use DB;
use Illuminate\Http\Request;
use Validator;

class SysflowController extends Controller {
	private $company;
	private $user;
	private $role_flag;
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$getUser = \Auth::guard('api')->user();
			$user = User::find($getUser->id);
			if (!$this->getDevice()) {
				//app
				$user->company_id = $user->appcompany_id;
			}
			$this->user = $user;
			$company = \App\Models\Company::find($this->user->company_id);
			$this->role_flag = $company->role_flag;
			$this->company = $company;
		}
	}
	public function startFlow(Request $request, $id, $type) {
		if (empty($id) || empty($type)) {
			return $this->respond('empty_error');
		}
		if (!in_array($type, [1, 2])) {
			return $this->respond('empty_error');
		}

		$resume = Resume::find($id);
		if (empty($resume)) {
			return $this->respond('get_error');
		}
		$get_status = DB::select('select id from resumes where id=? and step_id>=1 and (ask_status!=2||ask_status=0) and (check_status=0||check_status=3) limit 1', [$id]);
		//进入流程
		if ($type == 2 || !empty($get_status) || $resume->check_status == 1) {
			$info = FlowHistory::where(['resume_id' => $id, 'count' => $resume->count]
			)->select('id', 'admin_name as name', 'stime', 'etime', 'step_id', 'step_name', 'check_status', 'ask_status', 'count')->orderBy('id', 'desc')->get();
			DB::commit();
			return $this->respond('op_ok', $info);
		}
		//启动流程
		if ($type == 1) {
			$user = $this->user;
			$user->guard_name = $this->role_flag;
			if ($user->id != $resume->owner_id) {
				return $this->respond('no_auth');
			} else {
				$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
				if (!empty($staff) && $staff->company_id == $user->company_id) {
					if (!$user->can('flow_start') && !$user->hasRole('管理员')) {
						return $this->respond('no_auth');
					}
				} else {
					if (!$user->can('flow_start')) {
						return $this->respond('no_auth');
					}
				}
			}
			$time = time();
			$step = Step::find(1);
			$flow = [
				'owner_id' => $user->id,
				'owner_name' => $user->name,
				'admin_id' => $user->id,
				'admin_name' => $user->name,
				'resume_id' => $id,
				'company_id' => $this->company->id,
				'company_name' => $this->company->name,
				'stime' => $time,
				'count' => $resume->count + 1,
				'step_id' => 1,
				'ask_status' => 1,
				'step_name' => $step->name,
				'created_at' => $time,
				'updated_at' => $time,
			];
			$count = $resume->count;
			$resume->count = $count + 1;
			$resume->step_id = 1;
			$resume->step_name = $step->name;
			$resume->ask_status = 1;
			$resume->check_status = 0;
			$resume->team_id = 0;
			DB::beginTransaction();
			try {
				//中间逻辑代码
				$get_flow = DB::table('flow_histories')->insertGetId($flow);
				$resume->save();
				$info = FlowHistory::where(['resume_id' => $id, 'count' => $count + 1])->select('id', 'admin_name as name', 'stime', 'etime', 'step_id', 'step_name', 'check_status', 'ask_status', 'count')->get();
				DB::commit();
				return $this->respond('add_ok', $info);
			} catch (\Exception $e) {
				//接收异常处理并回滚
				DB::rollBack();
				return $this->respond('add_error');
			}
		}
	}

	//修改流程状态
	public function move(Request $request, $id) {
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$flow_histories = FlowHistory::find($id);
		if (empty($flow_histories)) {
			return $this->respond('get_error');
		}
		if (($flow_histories->ask_status == 1 || $flow_histories->ask_status == 2) && ($flow_histories->check_status == 1 || $flow_histories->check_status == 2)) {
			return response()->json(['code' => 500, 'msg' => '该流程状态已更新，请刷新页面！']);
		}
		$resume = Resume::find($flow_histories->resume_id);
		$o_id = $resume->owner_id;
		$o_user = User::find($o_id);
		$step = $flow_histories->step_id;
		$user = $this->user;
		$owner_id = $flow_histories->owner_id;
		$admin_id = $flow_histories->admin_id;
		$user->guard_name = $this->role_flag;
		$get_resume_status = $flow_histories->ask_status;
		$status = $request->input('status');
		$reason = $request->input('reason');
		$check_email = \App\Models\EmailSet::where(['is_use' => 1, 'company_id' => $this->company->id])->first();
		$email_flag = $request->input('flag');
		$offer_data = [];
		$flow = [];
		$time = time();
		$answer = new \App\Models\Answer();
		//根据step_id 判断权限
		if (!in_array($status, [1, 2, 3])) {
			return $this->respond('empty_error');
		}
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if ($step == 1) {
			if (($user->id != $owner_id)) {
				return $this->respond('resume_owner_no_auth');
			} else {
				if (!empty($staff) && $staff->company_id == $user->company_id) {
					if (!$user->can('check_first') && !$user->hasRole('管理员')) {
						return $this->respond('no_auth');
					}
				} else {
					if (!$user->can('check_first')) {
						return $this->respond('no_auth');
					}
				}
			}
		} else if ($step == 2) {
			if (($user->id != $owner_id)) {
				return $this->respond('resume_owner_no_auth');
			} else {
				if (!empty($staff) && $staff->company_id == $user->company_id) {
					if (!$user->can('send_offer') && !$user->hasRole('管理员')) {
						return $this->respond('no_auth');
					}
				} else {
					if (!$user->can('send_offer')) {
						return $this->respond('no_auth');
					}
				}
			}
		} else if ($step == 3) {
			if (($user->id != $owner_id)) {
				return $this->respond('resume_owner_no_auth');
			} else {
				if (!empty($staff) && $staff->company_id == $user->company_id) {
					if (!$user->can('job') && !$user->hasRole('管理员')) {
						return $this->respond('no_auth');
					}
				} else {
					if (!$user->can('job')) {
						return $this->respond('no_auth');
					}
				}
			}
		} else if ($step > 3) {
			if ($flow_histories->ask_status == 0) {
				if (($user->id != $owner_id)) {
					return $this->respond('resume_owner_no_auth');
				} else {
					if (!empty($staff) && $staff->company_id == $user->company_id) {
						if (!$user->can('call') && !$user->hasRole('管理员')) {
							return $this->respond('no_auth');
						}
					} else {
						if (!$user->can('call')) {
							return $this->respond('no_auth');
						}
					}
				}
			} else if ($flow_histories->ask_status == 1) {
				$get_step_role_flag = "sys_step_op_" . $step;
				if (($user->id != $admin_id)) {
					return $this->respond('no_auth');
				} else {
					if (!empty($staff) && $staff->company_id == $user->company_id) {
						if (!$user->can($get_step_role_flag) && !$user->hasRole('管理员')) {
							return $this->respond('no_auth');
						}
					} else {
						if (!$user->can($get_step_role_flag)) {
							return $this->respond('no_auth');
						}
					}
				}
			}

		}
		//根据status 赋值 未邀约和待定
		if ($get_resume_status == 0 || $get_resume_status == 3) {
//系统默认-待邀约
			$flow_histories->ask_status = $status;
			$resume->ask_status = $status;
			//收集面试相关信息
			if ($step > 3 && $status == 1) {
//自定义-待邀约
				$validator = Validator::make($request->all(),
					['adminId' => 'required', 'adminName' => 'required|max:20', 'address' => 'required|max:255', 'sTime' => 'required|integer:10', 'eTime' => 'required|integer:10'],
					['adminId.required' => '操作人id不能为空', 'adminName.required' => '操作人姓名不能为空', 'adminName.max' => '操作人姓名最多20个字符', 'address.required' => '面试地点不能为空', 'address.max' => '面试地点最多255个字符', 'sTime.required' => '面试开始日期不能为空',
						'sTime.integer' => '面试开始日期格式不正确', 'eTime.required' => '面试结束日期不能为空',
						'eTime.integer' => '面试结束日期格式不正确']);
				if ($validator->fails()) {
					return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
				}

				$flow_histories->admin_id = $request->input('adminId');
				$flow_histories->admin_name = $request->input('adminName');
				$flow_histories->address = $request->input('address');
				$flow_histories->stime = $request->input('sTime');
				$flow_histories->etime = $request->input('eTime');
			}

		} else if ($get_resume_status == 1) {
			//通过
			//待面试
			$flow_histories->check_status = $status;
			if ($status != 1) {
				$resume->check_status = $status;
			} else {
				$get_step_name;
				$get_step_id;
				$get_ask_status;
				$get_flow_id = $flow_histories->flow_id;
				$get_flow_name = $flow_histories->flow_name;
				if ($step == 1) {
					$flow_id = $request->input('flowId');
					if (empty($flow_id)) {
						return response()->json(['status' => 500, 'msg' => '流程id不能为空']);
					}
					$get_flow = Flow::find($flow_id);
					$get_step = Step::where(['flow_id' => $flow_id, 'status' => 0])->first();
					$get_flow_id = $flow_id;
					$get_flow_name = $get_flow->name;
					if (!empty($get_step)) //流程有步骤时
					{
						$get_step_id = $get_step->id;
						$get_step_name = $get_step->name;
						$resume->flow_id = $flow_id;
						$resume->step_id = $get_step_id;
						$resume->step_name = $get_step_name;
						$resume->ask_status = 0;
						$get_ask_status = 0;
					} else {
						//流程没有步骤时
						$resume->ask_status = 1;
						$resume->check_status = 0;
						$resume->step_id = 2;
						$get_step = Step::find(2);
						$resume->step_name = $get_step->name;
						$resume->flow_id = $flow_id;
						$get_step_id = 2;
						$get_step_name = $get_step->name;
						$get_ask_status = 1;
					}

				} else if ($step == 2) {
					//发放offer
					$validator = Validator::make($request->all(),
						['offerMobile' => 'required|regex:/^[1][0-9]{10}$/', 'offerEmail' => 'required|email', 'offerDate' => 'required|integer:10', 'offerSalary' => 'required|max:64', 'offerAddress' => 'required|max:255', 'offerDept' => 'required|max:255', 'offerPosition' => 'required|max:255', 'offerPerson' => 'required|max:20'],
						['offerMobile.required' => '联系人电话不能为空', 'offerMobile.regex' => '联系人电话格式不正确', 'offerEmail.required' => '邮箱不能为空', 'offerEmail.email' => '邮箱格式不正确', 'offerDate.required' => '入职日期不能为空', 'offerDate.integer' => '入职日期格式不正确', 'offerSalary.required' => '薪资不能为空', 'offerSalary.max' => '薪资最多64个字符', 'offerAddress.required' => '地点不能为空',
							'offerAddress.max' => '地点最多255个字符', 'offerDept.required' => '部门不能为空',
							'offerDept.max' => '部门最多255个字符', 'offerPosition.required' => '职位不能为空',
							'offerPosition.max' => '职位最多255个字符', 'offerPerson.required' => '联系人姓名不能为空',
							'offerPerson.max' => '联系人姓名最多20个字符']);
					if ($validator->fails()) {
						return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
					}
					if (empty($check_email)) {
						if ($email_flag == 1) {
							$offer_data = [
								'fid' => $id,
								'resume_id' => $resume->id,
								'name' => $resume->name,
								'user_id' => $this->user->id,
								'company_id' => $this->company->id,
								'mobile' => $request->input('offerMobile'),
								'email' => $request->input('offerEmail'),
								'entry_time' => $request->input('offerDate'),
								'salary' => $request->input('offerSalary'),
								'address' => $request->input('offerAddress'),
								'dept' => $request->input('offerDept'),
								'position' => $request->input('offerPosition'),
								'person' => $request->input('offerPerson'),
								'created_at' => $time,
								'updated_at' => $time,
							];
						} else {
							return response()->json(['code' => 501, 'msg' => '邮件服务器没有配置或启用,是否继续！']);
						}
					} else {
						$offer_data = [
							'fid' => $id,
							'resume_id' => $resume->id,
							'name' => $resume->name,
							'user_id' => $this->user->id,
							'company_id' => $this->company->id,
							'mobile' => $request->input('offerMobile'),
							'email' => $request->input('offerEmail'),
							'entry_time' => $request->input('offerDate'),
							'salary' => $request->input('offerSalary'),
							'address' => $request->input('offerAddress'),
							'dept' => $request->input('offerDept'),
							'position' => $request->input('offerPosition'),
							'person' => $request->input('offerPerson'),
							'created_at' => $time,
							'updated_at' => $time,
						];
					}

					$resume->step_id = 3;
					$get_step = Step::find(3);
					$resume->step_name = $get_step->name;
					$get_step_id = 3;
					$get_step_name = $get_step->name;
					$get_ask_status = 1;

				} else if ($step == 3) {

					$resume->check_status = 1;
					$resume->is_job = 3;
					$get_step = Step::find(3);
					$get_step_id = 3;
					$get_step_name = $get_step->name;
					$get_ask_status = 1;

				} else if ($step > 3) {
					//更新简历状态
					$check_step = Step::where(['status' => 0, 'flow_id' => $flow_histories->flow_id])->where('id', '>', $step)->first(); //是否为最后一个步骤
					if (!empty($check_step)) {
						$resume->ask_status = 0;
						$resume->check_status = 0;
						$resume->step_id = $check_step->id;
						$resume->step_name = $check_step->name;
						$get_step_id = $check_step->id;
						$get_step_name = $check_step->name;
						$get_ask_status = 0;

					} else {
						$resume->ask_status = 1;
						$resume->check_status = 0;
						$resume->step_id = 2;
						$get_step = Step::find(2);
						$resume->step_name = $get_step->name;
						$get_step_id = 2;
						$get_step_name = $get_step->name;
						$get_ask_status = 1;

					}
				}

				$flow = [
					'owner_id' => $o_id,
					'owner_name' => $o_user->name,
					'admin_id' => $o_id,
					'admin_name' => $o_user->name,
					'resume_id' => $flow_histories->resume_id,
					'company_id' => $this->company->id,
					'company_name' => $this->company->name,
					'stime' => $time,
					'count' => $flow_histories->count,
					'step_id' => $get_step_id,
					'step_name' => $get_step_name,
					'flow_id' => $get_flow_id,
					'flow_name' => $get_flow_name,
					'ask_status' => $get_ask_status,
					'created_at' => $time,
					'updated_at' => $time,
				];
			}
			if ($step > 3) {
				//自定义流程评分
				//记录评价数据
				$weight = $request->input('key');
				$score = $request->input('value');
				$vid = $request->input('vid');
				if (!empty($vid)) {
					//步骤有评价表时进行评分处理
					$remark = $request->input('remark');
					if (empty($id) || !is_array($weight) || !is_array($score) || empty($vid)) {
						return $this->respond('empty_error');
					}
					if (count($weight) != count($score)) {
						return $this->respond('empty_error');
					}

					$total_score = 0;
					foreach ($score as $key => $value) {
						$score[$key] = intval($value);
						$total_score += ($weight[$key] / 5) * $value;
					}
					$answer->evaluate_id = $vid;
					$answer->resume_id = $flow_histories->resume_id;
					$answer->flow_id = $flow_histories->flow_id;
					$answer->step_id = $flow_histories->step_id;
					$answer->value = json_encode($score);
					$answer->score = sprintf("%.2f", $total_score);
					$answer->count = $flow_histories->count;
					$answer->content = $remark;
				}
			}

		}
		if ($status != 1) {
			//未通过和待定添加原因
			$validator = Validator::make($request->all(),
				['reason' => 'required|max:255'],
				['reason.required' => '原因不能为空', 'reason.max' => '原因最多255个字符']);
			if ($validator->fails()) {
				return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
			}
			$flow_histories->reason = json_encode($reason);
		}
		try {
			$offer_id = 0;
			//中间逻辑代码
			if ($step != 3 && $get_resume_status == 1 && $status == 1) {
				//流程历史记录表新增记录
				DB::table('flow_histories')->insertGetId($flow);
			}
			if ($step == 2 && (!empty($check_email) || $email_flag == 1) && $get_resume_status == 1 && $status == 1) //offer记录表新增数据
			{
				$offer_id = DB::table('offers')->insertGetId($offer_data);
			}
			$flow_histories->save();
			$resume->save();
			$info = FlowHistory::where(['resume_id' => $flow_histories->resume_id, 'count' => $flow_histories->count])->select('id', 'admin_name as name', 'stime', 'etime', 'step_id', 'step_name', 'check_status', 'ask_status', 'count')->orderBy('id', 'desc')->get();
			if ($step > 3 && $get_resume_status == 1) {
				if (!empty($answer->toArray())) {
					$answer->save();
				}
			}
			DB::commit();
			if ($step == 2 && (!empty($check_email) || $email_flag == 1)) {
				//发送邮件代码
				$check_email = \App\Models\EmailSet::where(['is_use' => 1, 'company_id' => $this->company->id])->first();
				if (!empty($offer_id) && !empty($check_email)) {
					$this->sendOfferInfo($offer_id);
				}
			}
			$data['info'] = $info;
			$uid = $request->input('adminId') ? $request->input('adminId') : $flow_histories->admin_id;
			$data['chat'] = ['resume_id' => $flow_histories->resume_id, 'uid' => $uid];
			return $this->respond('op_ok', $data);
		} catch (\Exception $e) {
			//接收异常处理并回滚
			DB::rollBack();
			\Log::info($e);
			return $this->respond('op_error');
		}
	}
	//获取流程历史表面试官相关信息
	public function get(Request $request, $id, $type) {
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		if (!in_array($type, [1, 2])) {
			return $this->respond('empty_error');
		}
		$flow_histories = FlowHistory::where(['id' => $id, 'status' => 0])->select('id', 'stime', 'etime', 'address', 'admin_id', 'admin_name', 'step_id')->first();
		if (empty($flow_histories)) {
			return $this->respond('get_error');
		}
		$address = [];
		$step = Step::find($flow_histories->step_id);
		$role_id = $step->role_id;
		$roles = [];
		if (!empty($role_id)) {
			$roles = DB::table('model_has_roles as a')->join('users as b', 'a.model_id', '=', 'b.id')
				->join('staff as c', 'c.user_id', '=', 'b.id')
				->where(['a.role_id' => $role_id, 'b.status' => 0, 'c.status' => 0])->distinct('b.id')->select('b.id', 'b.name')->get();
		}
		$data['item'] = $flow_histories;
		$data['user'] = $roles;
		if ($type == 1) {
			$address = \App\Models\Address::where(['status' => 0, 'company_id' => $this->company->id])->select('id', 'address')->orderBy('id', 'desc')->get();
			$data['address'] = $address;
		}
		return $this->respond('get_ok', $data);
	}
	//修改流程历史表面试官相关信息
	public function put(Request $request, $id) {
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$model = FlowHistory::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (($user->id != $model->owner_id)) {
			return $this->respond('no_auth');
		} else {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('interview_edit') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('interview_edit')) {
					return $this->respond('no_auth');
				}
			}
		}
		$validator = Validator::make($request->all(),
			['adminId' => 'required', 'adminName' => 'required|max:20', 'sTime' => 'required|integer:10', 'eTime' => 'required|integer:10', 'address' => 'required|max:255'],
			['adminId.required' => '操作人id不能为空', 'adminName.required' => '操作人姓名不能为空', 'adminName.max' => '操作人姓名最多20个字符', 'sTime.required' => '面试开始日期不能为空',
				'sTime.integer' => '面试开始日期格式不正确', 'eTime.required' => '面试结束日期不能为空',
				'eTime.integer' => '面试结束日期格式不正确', 'address.required' => '面试地点不能为空', 'address.max' => '面试地点最多255个字符']);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$model->admin_id = $request->input('adminId');
		$model->admin_name = $request->input('adminName');
		$model->stime = $request->input('sTime');
		$model->etime = $request->input('eTime');
		$model->address = $request->input('address');
		$result = $model->save();
		if ($result) {
			return $this->respond('update_ok');
		}
		return $this->respond('update_error');
	}
	//获取步骤未通过原因
	public function getReason(Request $request, $id) {
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$model = Reason::where(['step_id' => $id, 'status' => 0])->orderBy('id', 'desc')->pluck('name');
		return $this->respond('get_ok', $model);
	}
	//获取历史流程记录
	public function all(Request $request, $id) {
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$resume = Resume::find($id);
		if (empty($resume)) {
			return $this->respond('get_error');
		}
		$get_status = DB::select('select id from resumes where id=? and step_id>=1 and status=0 and (ask_status=2||check_status=2) limit 1', [$id]);
		$query = FlowHistory::where(['resume_id' => $id]);
		if (empty($get_status) || $resume->check_status == 1) {
			$query = $query->where('count', '<', $resume->count);
		}
		$info = $query->select('id', 'admin_name as name', 'stime', 'etime', 'step_id', 'step_name', 'check_status', 'ask_status', 'count')->orderBy('count', 'desc')->orderBy('id', 'desc')->get()->toArray();
		$num = 0;
		$item = array();
		if (!empty($info)) {
			$get_count = $info[0]['count'];
			foreach ($info as $key => $value) {
				if ($get_count == $value['count']) {
					$item[$num][] = $value;
				} else {
					$get_count = $info[$key]['count'];
					$num++;
					$item[$num][] = $value;
				}
			}
		}
		$data['item'] = $item;
		return $this->respond('get_ok', $data);
	}
	//获取流程详细信息
	public function getSingle(Request $request, $id, $type) {
		if (!in_array($type, [1, 2])) {
			return $this->respond('empty_error');
		}
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$model = FlowHistory::where(['id' => $id, 'status' => 0])->select('id', 'admin_name as name', 'stime', 'etime', 'step_id', 'step_name', 'check_status', 'ask_status', 'count', 'reason', 'resume_id', 'owner_id')->first();
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$step_id = $model->step_id;
		if ($type == 1) {
			//权限判断
			$user = $this->user;
			$user->guard_name = $this->role_flag;
			$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
			$check_status = $model->check_status;
			$ask_status = $model->ask_status; //其他查看判断权限
			$get_view_role_flag = '';
			if ($step_id == 1) {
				if ($check_status == 3 || $ask_status == 3) {
					//待定查看只判断所有者
					$get_view_role_flag = 'check_first';

				} else {
					$get_view_role_flag = 'check_first_view';
				}
			} else if ($step_id == 2) {
				//发送offer 简历拥有着+权限
				if ($check_status == 3 || $ask_status == 3) {
					//待定查看只判断所有者
					$get_view_role_flag = 'send_offer';

				} else {
					$get_view_role_flag = 'send_offer_view';
				}
				if ($user->id != $model->owner_id) {
					return $this->respond('no_auth');
				}
			} else if ($step_id == 3) {
				//发送offer 简历拥有着+权限
				if ($check_status == 3 || $ask_status == 3) {
					//待定查看只判断所有者
					$get_view_role_flag = 'job';

				} else {
					$get_view_role_flag = 'job_view';
				}
			} else if ($step_id > 3) {
				if ($check_status == 3 || $ask_status == 3) {
					//待定查看只判断所有者
					$get_view_role_flag = 'sys_step_op_' . $step_id;

				} else {
					$get_view_role_flag = 'sys_step_view_' . $step_id;
				}
			}
			//权限判断
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can($get_view_role_flag) && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can($get_view_role_flag)) {
					return $this->respond('no_auth');
				}
			}
		}
		if (!empty($model->reason)) {
			$model->reason = json_decode($model->reason);
		} else {
			$model->reason = [];
		}
		$problem = [];
		$get_score = null;
		$get_offer = '';
		$v_id = 0;
		if ($step_id > 3) {
			$get_evaluaty = \App\Models\Evaluaty::where('step_id', 'like', '%,' . $step_id . ',%')->orderBy('id', 'desc')->first();
			if (!empty($get_evaluaty)) {
				$v_id = $get_evaluaty->id;
				$get_title = \App\Models\Title::where(['evaluate_id' => $v_id, 'status' => 0])->select('id', 'name')->get();
				if (!empty($get_title)) {
					foreach ($get_title as $key => $value) {
						$title_id = $value->id;
						$problem[$key]['id'] = $title_id;
						$problem[$key]['name'] = $value->name;
						$problem[$key]['title'] = \App\Models\Subject::where(['title_id' => $title_id, 'status' => 0])->select('id', 'name', 'weight')->get();
					}
				}
			}
			$count = $model->count;
			$get_score = \App\Models\Answer::where(['evaluate_id' => $v_id, 'count' => $count, 'resume_id' => $model->resume_id, 'step_id' => $step_id, 'status' => 0])->orderBy('id', 'desc')->first();
			if (!empty($get_score)) {
				if (!empty($get_score->value)) {
					$get_score->value = json_decode($get_score->value);
				} else {
					$get_score->value = [];
				}
				if (empty($get_score->content)) {
					$get_score->content = '';
				}
			}
		}
		if ($step_id == 2 && $model->check_status == 1) {
			$get_offer = \App\Models\Offer::where(['status' => 0, 'fid' => $id])->first();
		}
		$model->vid = $v_id;
		$data['item'] = $model;
		$data['problem'] = $problem;
		$data['answer'] = $get_score;
		$data['offer'] = $get_offer;
		return $this->respond('get_ok', $data);
	}
	//再次发送offer
	public function sendOffer(Request $request, $id) {
		\Log::info('发送邮件开始 date' . date('Y-m-d H:i:s'));
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'user_id' => $user->id])->first();
		if ($staff->company_id == $user->company_id) {
			if (!$user->can('send_offer') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('send_offer')) {
				return $this->respond('no_auth');
			}
		}
		if (empty($id)) {
			return $this->respond('empty_error');
		}
		$offer = \App\Models\Offer::where(['status' => 0, 'fid' => $id])->orderBy('id', 'desc')->first();
		if (empty($offer)) {
			return $this->respond('get_error');
		}
		$check_email = \App\Models\EmailSet::where(['is_use' => 1, 'company_id' => $this->company->id])->first();
		if (empty($check_email)) {
			return response()->json(['code' => 501, 'msg' => '邮件服务器没有配置或启用']);
		}
		//发送邮件
		\Log::info('调用任务队列开始 date' . date('Y-m-d H:i:s'));
		$this->sendOfferInfo($offer->id);
		return $this->respond('op_ok');
	}

}
